﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;


namespace NotebookS2
{
    public partial class M_BANK_ACCOUNT : RibbonForm
    {
        public M_BANK_ACCOUNT()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        
        void BindBanks()
        {
            BankManager oManager = new BankManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable tbl = oManager.GetBanks();

            oManager.Close();

            cboBankCodes.Properties.Items.Clear();
            foreach (DataRow oRow in tbl.Rows)
            {
                cboBankCodes.Properties.Items.Add(oRow["BankCode"].ToString());
            }
            if (cboBankCodes.Properties.Items.Count > 0)
            {
                cboBankCodes.SelectedIndex = 0;
            }

        }

        private void M_BANK_ACCOUNT_Load(object sender, EventArgs e)
        {
            BindBanks();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboBankCodes_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();

            BankManager oManager = new BankManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            tbl = oManager.GetByBankCode(cboBankCodes.Text);

            oManager.Close();

            if (tbl.Rows.Count > 0)
            {
                DataRow oRow = tbl.Rows[0];
                txtBankName.Text = oRow["BankName"].ToString();
            }

           

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                BankAccountManager oManager = new BankAccountManager();
                BankAccountUnit oUnit = new BankAccountUnit();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.BankCode = cboBankCodes.Text;
                oUnit.BankName = txtBankName.Text;

                oUnit.BranchCode = txtBranchCode.Text;
                oUnit.BranchName = txtBranchName.Text;

                oUnit.AccountNumber = txtBankAccountNumber.Text;
                oUnit.AcctCode = txtGLAccountCode.Text;
                oUnit.AcctName = txtAcctName.Text;

                oManager.AddBankAccount(oUnit);

                oManager.Close();


                cboBankCodes.Enabled = false;
                txtBankName.Enabled = false;

                txtBranchCode.Enabled = false;
                //txtBranchName.Enabled = false;

                //txtGLAccountCode.Enabled = false;
                //txtBankAccountNumber.Enabled = false;

                btnAdd.Text = "Update";
                MessageBox.Show("Bank Account successfully added!!!", "Bank Account", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                BankAccountManager oManager = new BankAccountManager();
                BankAccountUnit oUnit = new BankAccountUnit();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.BankCode = cboBankCodes.Text;
                oUnit.BankName = txtBankName.Text;

                oUnit.BranchCode = txtBranchCode.Text;
                oUnit.BranchName = txtBranchName.Text;

                oUnit.AccountNumber = txtBankAccountNumber.Text;
                oUnit.AcctCode = txtGLAccountCode.Text;
                oUnit.AcctName = txtAcctName.Text;

                oManager.UpdateBankAccount(oUnit);

                oManager.Close();


                cboBankCodes.Enabled = false;
                txtBankName.Enabled = false;

                txtBranchCode.Enabled = false;
                //txtBranchName.Enabled = false;

                //txtGLAccountCode.Enabled = false;
                //txtBankAccountNumber.Enabled = false;

                btnAdd.Text = "Update";

                MessageBox.Show("Bank Account successfully updated!!!", "Bank Account", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            FND_BANK_ACCOUNT oForm = new FND_BANK_ACCOUNT();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            if (oForm.SelectedBankCode != null)
            {
                cboBankCodes.Text = oForm.SelectedBankCode;
                txtBankName.Text = oForm.SelectedBankName;

                txtBranchCode.Text = oForm.SelectedBranchCode;
                txtBranchName.Text = oForm.SelectedBranchName;

                txtGLAccountCode.Text = oForm.SelectedGLAccountCode;
                txtAcctName.Text = oForm.SelectedGLAccountName;
                txtBankAccountNumber.Text = oForm.SelectedAccountNumber;


                cboBankCodes.Enabled = false;
                txtBankName.Enabled = false;

                txtBranchCode.Enabled = false;
                //txtBranchName.Enabled = false;

                //txtGLAccountCode.Enabled = false;
                //txtBankAccountNumber.Enabled = false;

                btnAdd.Text = "Update";
            }
        }

        private void txtGLAccountCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;

            oForm.ShowDialog();

            if (oForm.SelectedGLAcctCode != null)
            {
                txtGLAccountCode.Text = oForm.SelectedGLAcctCode;
                txtAcctName.Text = oForm.SelectedGLAcctName;
            }

        }
    }
}
